use alloc::alloc::handle_alloc_error;
use core::alloc::{GlobalAlloc, Layout};
use core::ptr::null_mut;
use crate::log_println;
use crate::tools::handle_alloc_error_with_log;

/// mimalloc.
pub struct MiMalloc;

impl MiMalloc {
    pub const fn new() -> Self {
        Self {}
    }
}

unsafe impl GlobalAlloc for MiMalloc {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        log_println!("alloc layout: {:?}", layout);
        // check max align
        // let align = layout.align();
        // if align > MAX_SUPPORTED_ALIGN {
        //     return null_mut();
        // }

        null_mut()
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        log_println!("dealloc");
        handle_alloc_error_with_log(layout, "Dealloc not implemented yet.");
    }
}

// TODO: Sync Trait
// unsafe impl Sync for MiMalloc {}
